<template>
  <div class="app-container">
    <el-card>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small">
          <el-form-item label="话题名称：">
            <el-input
              placeholder="请输入话题名称"
              v-model="listQuery.conditionsName"
              maxlength="20"
              clearable
            >
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              icon="el-icon-refresh"
              style=""
              @click="handleResetSearch()"
              size="small"
              >重置</el-button
            >
            <el-button
              icon="el-icon-search"
              type="primary"
              @click="handleSearchList()"
              size="small"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button
          @click="addDialog()"
          size="mini"
          class="btn-add"
          type="primary"
          icon="el-icon-plus"
          >添加社区话题</el-button
        >
      </div>
      <div class="table-container">
        <el-table
          ref="resourceTable"
          :data="list"
          style="width: 100%"
          v-loading="listLoading"
          border
        >
          <el-table-column label="ID" width="100" align="center">
            <template slot-scope="scope">{{ scope.row.id }}</template>
          </el-table-column>
          <el-table-column label="话题名称" align="center">
            <template slot-scope="scope">{{ scope.row.name }}</template>
          </el-table-column>
          <el-table-column label="排序" prop="sort" align="center">
          </el-table-column>
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <el-tag :type="scope.row.enableStatus ? 'success' : 'danger'">{{
                scope.row.enableStatus == 1 ? "启用" : "禁用"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="文章数" align="center">
            <template slot-scope="scope">{{
              scope.row.invitationCount
            }}</template>
          </el-table-column>

          <el-table-column label="点击量" prop="hotCount" align="center">
          </el-table-column>
          <el-table-column label="发帖量" prop="invitationCount" align="center">
          </el-table-column>
          <el-table-column label="热度" prop="heatCount" align="center">
          </el-table-column>
          <el-table-column label="添加时间" width="160" align="center">
            <template slot-scope="scope">{{ scope.row.createTime }}</template>
          </el-table-column>
          <el-table-column label="操作" width="140" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.$index, scope.row)"
              >
                修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.$index, scope.row)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pageRight">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes,prev, pager, next,jumper"
          :current-page.sync="listQuery.pageNum"
          :page-size="listQuery.pageSize"
          :page-sizes="[10, 15, 20]"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-dialog
      :title="addForm.id ? '修改话题' : '添加话题'"
      :visible.sync="addDialogDisplay"
      @close="handleClose()"
      width="500px"
      center
    >
      <div>
        <el-form ref="form" :model="addForm" :rules="rules" label-width="80px">
          <el-form-item label="话题名称" prop="name">
            <el-input
              placeholder="请输入话题名称"
              style="width: 300px"
              v-model="addForm.name"
            ></el-input>
          </el-form-item>

          <el-form-item label="排序" prop="sort">
            <el-input-number
              v-model="addForm.sort"
              :min="0"
              :max="9999"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="状态" prop="enableStatus">
            <el-radio-group
              style="width: 300px"
              placeholder="请选择状态"
              v-model="addForm.enableStatus"
            >
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose()">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  fetchList,
  createResource,
  updateResource,
  deleteResource
} from "@/api/topicArticles";
import { formatDate } from "@/utils/date";
export default {
  name: "resourceList",
  data() {
    return {
      listQuery: {
        pageNum: 1,
        pageSize: 10
      },
      list: null,
      total: null,
      listLoading: false,
      isEdit: false,
      categoryOptions: [],
      defaultCategoryId: null,
      rules: {
        name: [{ required: true, message: "请输入话题名称", trigger: "blur" }],
        enableStatus: [
          { required: true, message: "请选择状态", trigger: "blur" }
        ],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }]
      },
      addDialogDisplay: false,
      addForm: {
        name: "",
        enableStatus: 1,
        sort: 0
      }
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    }
  },
  methods: {
    handleClose() {
      this.$refs.form.resetFields();
      this.addForm = {
        name: "",
        enableStatus: "1",
        sort: 0
      };
      this.addDialogDisplay = false;
    },
    submitForm(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          if (this.addForm.id) {
            updateResource(this.addForm).then(res => {
              this.addDialogDisplay = false;
              this.$message.success("修改成功");
              this.getList();
            });
          } else {
            createResource(this.addForm).then(res => {
              this.addDialogDisplay = false;
              this.$message.success("添加成功");
              this.getList();
            });
          }
        }
      });
    },
    addDialog() {
      this.addDialogDisplay = true;
    },
    handleUpdate(index, row) {
      this.addDialogDisplay = true;
      this.addForm = Object.assign({}, row);
    },
    handleResetSearch() {
      this.listQuery = {
        pageNum: 1,
        pageSize: 10,
        conditionsName: "",
        groupId: ""
      };
      this.getList();
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleDelete(index, row) {
      console.log(row);
      this.$confirm(
        `是否要删除该话题${row.name}?,该话题下有${row.invitationCount}篇文章, `,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        deleteResource(row.id).then(response => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.getList();
        });
      });
    },
    getList() {
      this.listLoading = true;
      //获取页面列表
      fetchList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
      //获取条件分组
    }
  }
};
</script>
<style scoped>
.app-container {
  height: calc(100vh - 50px);
  background-color: #f5f5f5;
  padding: 20px;
}
</style>
